<?php
namespace app\sisome\controller;
use think\Controller;
use think\captcha\Captcha;
use think\Db;
class Publics extends Controller{
    public function _initialize(){
        if(!empty($_POST)){
            if (!isset($_POST['__token__'])) {
                $this->error('非法请求！');
            }else{
                if(session('__token__')<>$_POST['__token__']){
                    $this->error('请勿重复提交！');
                }
            }
        }
    }
    public function index(){
        $this->redirect ( 'index/index' );
    }
    public function login(){    //登陆
        if(!$_POST){
            if(islogin_admin()){
                $this->redirect ( 'index/index' );
            }else{
                return view();
            }
        }else{
            $uname=$_POST['uname'];
            $pwd=md5($_POST['pwd']);
            $yzm=$_POST['yzm'];
            if(!$this->chickverify($yzm)){
                $this->error('验证码错误！');
            }
            $aduser = db('aduser')->where(array('uname'=>$uname,'pwd'=>$pwd))->find();
            if(!$aduser){
                $this->error('帐号或密码错误！');
            }else{
                $ip = request()->ip();
                $logintime=time();
                //更新数据库
                Db::startTrans();   // 启动事务-------------
                $ret1=db('aduser')->where('uname', $uname)->update([
                    'lasttime' => $logintime,
                    'lastip' => $ip,
                    'loginnum' =>Db::raw('loginnum+1')
                ]);
                $loginid=db('loglogin')->insertGetId([
                    'uname'=>$aduser['uname'],
                    'time'=>$logintime,
                    'ip'=>$ip,
                    'type'=>1
                ]);
                if($ret1 && $loginid){
                    Db::commit();       // 提交事务-------------
                    /* 写入session */
                    session('aduid', $aduser['id']);
                    session('adpwd', MD5($aduser['pwd']));
                    session('aduname', $aduser['uname']);
                    session('adlasttime', $logintime);
                    session('adsession_id_dietime', 0); //0表示不记密码 浏览器关闭Cookie死掉 有效登陆按adlogintime最后在线时间核验
                    if(isset($_POST['autologin'])){    //自动登录
                        $adsession_id=cookie('PHPSESSID');   //cookie获取session_id
                        cookie('PHPSESSID', $adsession_id, 3600*24*7);  //设置cookie有效期为7天
                        session('adsession_id_dietime', time()+3600*24*7);   //记录过期时间 因为无法‘获取’cookie的过期时间
                        // echo "<script>  
                        // localStorage.setItem('username'," . "'" . $account . "'" . ");  
                        // localStorage.setItem('password'," . "'" . $_POST['password'] . "'" . ");  
                        // </script>";
                    }
                    $this->redirect('index/index');
                }else{
                    Db::rollback();     // 回滚事务-------------
                    $this->error('登陆失败！');
                }
            }
        }
    }
    public function logout(){    //用户退出登陆
        echo "<script> localStorage.clear(); </script>";        //清除本地账号密码存储
        session(null);
        cookie('PHPSESSID',null);
        $this->success('已安全退出',url('publics/login'));
    }
    //验证码
    public function verify(){
        $config =    [
            // 验证码位数
            'length'      =>    5,   
            // 关闭验证码杂点
            'useNoise'    =>    false, 
            'useCurve'    =>    false, 
            'codeSet'    =>    '0123456789', 
        ];

        $captcha = new Captcha($config);
        return $captcha->entry();
    }
    //核验验证码
    public function chickverify($code){
        // $code=request()->param()['code'];
        $captcha = new Captcha();
        return $captcha->check($code);
    }
}
